﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL.WebRuntime;
using BuXiaChu.OrderDishes.BLL;
using RestaurantNS = BuXiaChu.OrderDishes.Web.Admin.Modules.Restaurant;
using BuXiaChu.OrderDishes.Model;
namespace BuXiaChu.OrderDishes.Web.Admin.Restaurant
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetList : HttpHandlerBase, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);

            #region
            AjaxActionResult result = new AjaxActionResult();
            try
            {
                var pageCount = 0;
                var pageIndex = 0;
                var pageSize = 0;
                GetPageBarParams(out pageIndex, out pageSize);
                var sqlWhere = GetSearchCondition();
                UserControlPreviewManager<RestaurantNS.List> preivewManager = new UserControlPreviewManager<RestaurantNS.List>();
                RestaurantNS.List restaurantList = preivewManager.LoadUserControl("~/Modules/Restaurant/List.ascx");
                restaurantList.PageDataList = RestaurantManager.Search(sqlWhere, pageIndex, pageSize, out pageCount);
                PageBarModel pb = new PageBarModel();
                pb.Size = pageSize;
                pb.CurrentIndex = pageIndex;
                pb.Count = pageCount;
                pb.IsForAjax = true;
                pb.AjaxPageClickCallback = GetFormValue("ajaxCallback");
                restaurantList.PageBar = pb;
                result.message = preivewManager.RenderHtml(restaurantList);
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }
            SerializeDataToResponseStream(result);
            #endregion
        }

        private string GetSearchCondition()
        {
            #region
            var sqlWhere = " 1=1 ";
            var name = GetFormValue("name");
            var city = GetFormValue("city");
            var area = GetFormValue("area");
            var cat = GetFormValue("cat");

            if (string.IsNullOrEmpty(name) == false)
            {
                sqlWhere += " and name like'%" + name + "%'";
            }
            var cityId = 0;
            if (string.IsNullOrEmpty(city) == false && int.TryParse(city, out cityId)
                && cityId>0)
            {
                sqlWhere += " and CityId =" + city;
            }

            var areaId = 0;
            if (string.IsNullOrEmpty(area) == false && int.TryParse(area, out areaId)
                && areaId>0)
            {
                sqlWhere += " and AreaId =" + areaId;
            }

            var catId = 0;
            if (string.IsNullOrEmpty(cat) == false && int.TryParse(cat, out catId)
                 && catId > 0)
            {
                sqlWhere += " and Category =" + cat;
            }

            return sqlWhere;
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
